/**
 * 
 */
package com.live.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings.Secure;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import com.live.common.Constant;
import com.live.entity.User;
import com.live.service.PushService;
import com.live.tools.LiveTools;

/**
 * @author nhn
 * 
 */
public class LiveBaseActivity extends Activity {

	public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	static final int DATE_DIALOG_ID = 0;

	public Context context;

	public static List<Activity> activityList = new ArrayList<Activity>();

	public static List<Map<String, Object>> liveMatchList = null;

	public SharedPreferences store = null;

	// 0:未登录 1:登录
	public int userStatus = Constant.LOGOUT;

	public User user = new User();

	public boolean netIsOk = true;

	public String locale = null;

	public String deviceID;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;

		activityList.add(this);

		// 隐藏标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		// 网络连接判断
		if (!LiveTools.isConnect(context) && this.getClass() != LiveAlarmActivity.class) {
			netIsOk = false;
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setCancelable(false);
			builder.setTitle("请检查网络是否正确连接！");
			builder.setIcon(android.R.drawable.ic_dialog_info);
			builder.setPositiveButton("退出程序", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					userExit();
				}
			});
			builder.create().show();
		}

	}

	@Override
	protected void onResume() {
		super.onResume();

		store = getSharedPreferences(Constant.PREFS_NAME, MODE_PRIVATE);
		deviceID = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);

		// 启动推送服务
		Editor editor = store.edit();
		editor.putString(PushService.PREF_DEVICE_ID, deviceID);
		editor.commit();
		PushService.actionStart(getApplicationContext());

		// 用户登录判断
		if (store.getString("userId", null) != null) {
			userStatus = Constant.LOGIN;
		}

		// 获取本机语言环境
		locale = LiveTools.getLocaleLanguage();
	}

	@Override
	public void onConfigurationChanged(Configuration config) {
		super.onConfigurationChanged(config);
	}

	// 生成菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.user_menu, menu);
		return true;
	}

	// 初始化菜单
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);

		switch (userStatus) {
		case Constant.LOGIN:
			menu.getItem(0).setIcon(R.drawable.menu_user_off);
			menu.getItem(0).setTitle(LiveTools.tradToSimp("注销", locale));
			break;
		case Constant.LOGOUT:
			menu.getItem(0).setIcon(R.drawable.menu_user_login);
			menu.getItem(0).setTitle(LiveTools.tradToSimp("登录", locale));
			break;
		}

		return true;
	}

	// 菜单事件响应
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.userLogin:
			if (userStatus == Constant.LOGOUT) {

				// 跳转到登录页
				Intent intent = new Intent();
				intent.setClass(context, LiveUserLoginActivity.class);
				context.startActivity(intent);
			} else {

				// 注销
				userOff();
			}
			break;
		case R.id.userRegsit:

			// 跳转到注册页
			Intent intent = new Intent();
			intent.setClass(context, LiveUserRegistActivity.class);
			context.startActivity(intent);

			break;

		case R.id.userExit:

			// 退出应用
			userExit();
			break;
		}
		return false;
	}

	// 退出应用
	public void userExit() {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		if (Float.valueOf(Build.VERSION.RELEASE.substring(0, 3)) <= 2.1) {
			// Android2.1及以下版本退出
			activityManager.restartPackage(Constant.PREFS_NAME);
		} else {
			// Android2.2及以上版本退出
			for (int i = 0; i < activityList.size(); i++) {
				if (null != activityList.get(i)) {
					activityList.get(i).finish();
				}
			}
			activityManager.restartPackage(Constant.PREFS_NAME);
			// System.exit(0)会把启动的service给kill掉
			System.exit(0);
		}
	}

	// 用户注销
	public void userOff() {
		// 用户注销
		SharedPreferences userInfo = getSharedPreferences(Constant.PREFS_NAME, 0);
		SharedPreferences.Editor editor = userInfo.edit();
		editor.clear();
		editor.commit();

		// 提示注销成功
		Toast.makeText(this, LiveTools.tradToSimp("用户已注销！", locale), Toast.LENGTH_LONG).show();

		// 页面跳转到首页
		Intent intent = new Intent();
		intent.setClass(LiveBaseActivity.this, LiveMatchActivity.class);
		startActivity(intent);
	}
}
